﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CamHKVision;
using DevExpress.Data.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Internal;
using DevExpress.XtraPrinting.Native;
using FrameManager;
using HalconDotNet;
using ImageDefine;
using SmartDisplayControl;
using TableModule;

namespace CustomModule
{
    public static class CustomManager
    {
        public static SmartDisplayControl.SmartDisplayControl SmartDisplayControl { get; set; }
        public static bool bContinueRun = false;
        public static CamHKVision.CamHKVisionActioner camHKVisionActioner;
        public static  ZMotion.ZMotionCard zMotionCard;
        public static GeneralLib.HiPerfTimer HiPerfTimerGlueDummyShot;

        public static bool bRequestGlueWeight = false;
        public static bool bRequestPositionMatch = false;
        public static void Init()
        {

            FormStart formStart = new FormStart();
            FrameManager.FrameManager.FrmStart = formStart;
            FrameManager.MachineStatus.setHomeStart += MachineStatus_setHomeStart;

            FrameManager.MachineStatus.setMachineStart += MachineStatus_setMachineStart;
            FrameManager.MachineStatus.setMachineStop += MachineStatus_setMachineStop;
            System.Threading.Thread thread = new System.Threading.Thread(autoThreadFunction);
            thread.IsBackground = true;
            thread.Name = "AutoThread";
            thread.Start();

            System.Threading.Thread thread1 = new System.Threading.Thread(autoImageTragFunction);
            thread1.IsBackground = true;
            thread1.Name = "AutoImageTrag";
            thread1.Start();

            System.Threading.Thread thread2 = new System.Threading.Thread(autoGlueTimeFunction);
            thread2.IsBackground = true;
            thread2.Name = "AutoGlueTimeCal";
            thread2.Start();

            camHKVisionActioner = FrameManager.FrameManager.Camera1 as CamHKVision.CamHKVisionActioner;
            camHKVisionActioner.getNewFlyImage += CamHKVisionActioner_getNewFlyImage;

            zMotionCard = FrameManager.FrameManager.MotionCard as ZMotion.ZMotionCard;

            ///FrameManager.MachineStatus.bHomeed = true;
        }

        private static bool MachineStatus_setMachineStop()
        {
            zMotionCard.StopAdd();
            return true;
        }

        private static bool MachineStatus_setMachineStart()
        {

            //camHKVisionActioner.StartFlyGrab();
            //zMotionCard.StartRegist();
            zMotionCard.StartAdd();
            return true;
        }

        private static void CamHKVisionActioner_getNewFlyImage(HalconDotNet.HImage inputImage)
        {
            //ImageTask imageTask = ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect");
            //imageTask.RunFileImage(SmartDisplayControl, inputImage);
            //if (imageTask.bResultOK)
            //{
            //    double dTragPos = 0;
            //    bool bHaveTragPos=zMotionCard.GetRegistPos(out dTragPos);
            //    if (bHaveTragPos)
            //    {
            //        for (int i = 0; i < imageTask.ImageTaskResults.Count; i++)
            //        {
            //            ProductItem productItem = new ProductItem();
            //            productItem.TrigerBeltPosX = dTragPos;
            //            productItem.ImagePosX = imageTask.ImageTaskResults[i].ResultX;
            //            productItem.ImagePosY = imageTask.ImageTaskResults[i].ResultY;
            //            productItem.ImagePosU = imageTask.ImageTaskResults[i].ResultA;
            //            ConveyerManager.AddNewItem(productItem);
            //        }
            //    }
            //}
        }

        private static void autoThreadFunction()
        {
            HiPerfTimerGlueDummyShot = new GeneralLib.HiPerfTimer();
            HiPerfTimerGlueDummyShot.Start();
            while (true)
            {
                if (MachineStatus.bStart)
                {
                    AutoFunction();
                    //MachineStatus.bStart = false;
                }
                else
                {
                    HiPerfTimerGlueDummyShot.Start();
                }
                System.Threading.Thread.Sleep(1);
            }
        }
        public static void AutoFunction()
        {
            ProductItem productItem = GetActionItem();
            if (productItem != null)
            {

                double dCurrentPosBelt = ConveyerManager.CurrentPos;
                //启动皮带同步加速同步
                zMotionCard.StartMoveSync();
                while (true)
                {
                    //轨迹运动
                    double dLocalPosX = (productItem.ImagePosX + dCurrentPosBelt - productItem.TrigerBeltPosX);
                    double dLocalPosY = productItem.ImagePosY;
                    productItem.AreadyGlue = true;
                    GluePathModule.GluePathManager.SetGroupUnitCurrentCenter("Main", "Path1", dLocalPosX, dLocalPosY, productItem.ImagePosU);
                    GluePathModule.GluePathManager.SetGroupUnitCurrentTool("Main", "Path1", TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dX, TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dY);

                    int iGlueOuputPort = IOModule.IOManager.ioSetting.dicOutputSettings["GlueVa"].iChannal;
                    if (FrameManager.FrameManager.ProductSetting.禁止出胶)
                        iGlueOuputPort = 10;
                    GluePathActioner.GluePathAction.GluePathActionStart(TableManager.tableRunner.tableActioner, "Main", iGlueOuputPort);
                    System.Threading.Thread.Sleep(200);
                    zMotionCard.WaitCureMoveDone();

                    FrameManager.FrameManager.ProductSetting.Pass++;
                    //System.Threading.Thread.Sleep(3000);
                    productItem = GetActionItem();
                    if (productItem != null)
                    {
                        System.Threading.Thread.Sleep(100);
                        continue;


                    }
                    else
                    {
                        break;
                    }
                        
                }

                //停止皮带同步
                TableDataPosition position = TableManager.tableRunner.GetPosition("SafePos");
                zMotionCard.StopMoveSync(position.dXPos,position.dYPos);
                System.Threading.Thread.Sleep(300);
                GluePathActioner.GluePathAction.GluePathActionStop();
                //MarkTableAngleToMin();
                TableManager.tableRunner.SetSpd("LowSpd");
                TableManager.tableRunner.AbsMoveSingle(AxisName.Z,"SafePos");
                TableManager.tableRunner.SetSpd("HighSpd");
                TableManager.tableRunner.AbsGoXYU("SafePos");
                

                ProjectModule.ProjectManager.ProjectSetting.SingleProductGlueTime = GluePathActioner.GluePathAction.dTotalTimeGlue;
                HiPerfTimerGlueDummyShot.Start();
                //System.Threading.Thread.Sleep(1000);


                //回到开始位置
            }
            if (bRequestGlueWeight)
            {
                bRequestGlueWeight = false;
                GlueWeightAction();
            }
            if (FrameManager.FrameManager.ProductSetting.自动排胶间隔时间 > 1&& FrameManager.FrameManager.ProductSetting.禁止自动排胶==false)
            {
                if (HiPerfTimerGlueDummyShot.TimeUp(FrameManager.FrameManager.ProductSetting.自动排胶间隔时间))
                {
                    if (HaveUnGlueItem())
                    {
                        HiPerfTimerGlueDummyShot.Start();
                    }
                    else 
                    {
                        GlueDummyShotAction();
                        HiPerfTimerGlueDummyShot.Start();
                    } 
                }
                else
                {
                    BackToSafePostion();
                }
                
            }
            else
            {
                BackToSafePostion();
            }
        }

        public static void GlueDummyShotAction()
        {
            TableManager.tableRunner.SetSpd("HighSpd");
            TableManager.tableRunner.AbsGoXYU("排胶位置");
            TableManager.tableRunner.SetSpd("LowSpd");
            TableManager.tableRunner.Go("排胶位置");
            if(FrameManager.FrameManager.ProductSetting.禁止出胶==false)
                IOModule.IOManager.Output("GlueVa").ON = true;
            int iGlueTimeOn = (int)FrameManager.FrameManager.ProductSetting.自动排胶开胶时间 * 1000;
            System.Threading.Thread.Sleep(iGlueTimeOn);
            IOModule.IOManager.Output("GlueVa").OFF = true;
            int iGlueTimeOff = (int)FrameManager.FrameManager.ProductSetting.自动排胶关胶时间 * 1000;
            System.Threading.Thread.Sleep(iGlueTimeOff);
            TableManager.tableRunner.SetSpd("LowSpd");
            TableManager.tableRunner.AbsMoveSingle(AxisName.Z,"SafePos");
            //TableManager.tableRunner.Go("SafePos");

        }
        public static void BackToSafePostion()
        {

            if (HaveUnGlueItem())
            {
                
                TableDataPosition positionCurrent = TableManager.tableRunner.WhereTable();
                TableDataPosition positionSafePos = TableManager.tableRunner.GetPosition("SafePos");
                if (Math.Abs(positionCurrent.dXPos - positionSafePos.dXPos) > 1
                    || Math.Abs(positionCurrent.dYPos - positionSafePos.dYPos) > 1)
                {
                    IOModule.IOManager.Output("GlueVa").OFF = true;
                    TableManager.tableRunner.SetSpd("LowSpd");
                    TableManager.tableRunner.AbsMoveSingle(AxisName.Z, "SafePos");
                    TableManager.tableRunner.SetSpd("HighSpd");
                    TableManager.tableRunner.Go("SafePos");
                }         
            }
            
        }
        //private static void autoThreadFunction()
        //{
        //    while (true)
        //    {
        //        if (MachineStatus.bStart)
        //        {
        //            #region 点胶过程
        //            ImageTask imageTask = ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect");
        //            imageTask.Run(SmartDisplayControl);
        //            if (imageTask.bResultOK)
        //            {

        //                for (int i = 0; i < imageTask.ImageTaskResults.Count; i++)
        //                {
        //                    ProductItem productItemNew = new ProductItem();
        //                    productItemNew.TrigerBeltPosX = ConveyerManager.CurrentPos;
        //                    productItemNew.ImagePosX = imageTask.ImageTaskResults[i].ResultX;
        //                    productItemNew.ImagePosY = imageTask.ImageTaskResults[i].ResultY;
        //                    productItemNew.ImagePosU = imageTask.ImageTaskResults[i].ResultA;
        //                    //ConveyerManager.AddNewItem(productItemNew);
        //                    double dLocalPosX = (productItemNew.ImagePosX + ConveyerManager.CurrentPos - productItemNew.TrigerBeltPosX);
        //                    double dLocalPosY = productItemNew.ImagePosY;
        //                    GluePathModule.GluePathManager.SetGroupUnitCurrentCenter("Main", "Path1", dLocalPosX, dLocalPosY, productItemNew.ImagePosU);
        //                    GluePathModule.GluePathManager.SetGroupUnitCurrentTool("Main", "Path1", TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dX, TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dY);
        //                    GluePathActioner.GluePathAction.GluePathActionStart(TableManager.tableRunner.tableActioner, "Main", 7);

        //                    System.Threading.Thread.Sleep(200);
        //                    TableManager.tableRunner.WaitMoveDoneALL();
        //                    GluePathActioner.GluePathAction.GluePathActionStop();
        //                }

        //                //启动皮带同步加速同步
        //                //zMotionCard.StartMoveSync();

        //                //轨迹运动


        //                FrameManager.FrameManager.ProductSetting.Pass++;
        //                //System.Threading.Thread.Sleep(1000);

        //                //停止皮带同步
        //                //zMotionCard.StopMoveSync();
        //                //System.Threading.Thread.Sleep(100);
        //                MarkTableAngleToMin();
        //                TableManager.tableRunner.SetSpd("HighSpd");
        //                TableManager.tableRunner.Jump("SafePos");
        //                //System.Threading.Thread.Sleep(1000);


        //                //回到开始位置


        //            }
        //            MachineStatus.bStart = false;
        //            #endregion
        //            if (bRequestGlueWeight)
        //            {
        //                bRequestGlueWeight = false;
        //                GlueWeightAction();
        //            }

        //        }
        //        System.Threading.Thread.Sleep(10);
        //    }
        //}
        private static void autoImageTragFunction()
        {
            while (true)
            {
                if (MachineStatus.bStart)
                {
                    double dTragPos = 0;
                    zMotionCard.StartRegist();
                    while (true)
                    {
                        if (MachineStatus.bStart == false)
                        {
                            
                            break;
                        }
                        bool bHaveTragPos = zMotionCard.GetRegistPos(out dTragPos);
                        if (bHaveTragPos)
                        {
                            double dZoffSet = TableModule.TableManager.tableRunner.GetPosition("定位区域离皮带高度").dZPos;
                            ImageCalibModule.ImageCalibManager.ImageCalibHandeye.SetZOffSet(dZoffSet);
                            //dTragPos=ConveyerManager.CurrentPos
                            ImageTask imageTask = ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect");
                            imageTask.Run(SmartDisplayControl);
                            if (imageTask.bResultOK)
                            {
                                TableDataPosition OffSet = TableManager.tableRunner.GetPosition("偏移设定");
                                for (int i = 0; i < imageTask.ImageTaskResults.Count; i++)
                                {
                                    ProductItem productItemNew = new ProductItem();
                                    productItemNew.TrigerBeltPosX = dTragPos;
                                    if (FrameManager.MachineStatus.Is4Axis)
                                    {
                                        productItemNew.ImagePosX = imageTask.ImageTaskResults[i].ResultX + OffSet.dXPos;
                                        productItemNew.ImagePosY = imageTask.ImageTaskResults[i].ResultY + OffSet.dYPos;
                                        productItemNew.ImagePosU = imageTask.ImageTaskResults[i].ResultA;
                                    }
                                    else
                                    {
                                        
                                        productItemNew.ImagePosX = imageTask.ImageTaskResults[i].ResultX + TableManager.tableRunner.GetPosition("三轴相机中心和针头偏移").dXPos + OffSet.dXPos;
                                        productItemNew.ImagePosY = imageTask.ImageTaskResults[i].ResultY + TableManager.tableRunner.GetPosition("三轴相机中心和针头偏移").dYPos + OffSet.dYPos;
                                        productItemNew.ImagePosU = imageTask.ImageTaskResults[i].ResultA;
                                    }
                                    ConveyerManager.AddNewItem(productItemNew);
                                    FrameManager.FrameManager.ProductSetting.Input++;
                                }
                            }
                            if (FrameManager.FrameManager.ProductSetting.保存OK图片)
                            {
                                if (imageTask.bResultOK)
                                {
                                    SaveRunningImage(imageTask.currentImage, FrameManager.FrameManager.ProductSetting.保存图片路径, imageTask.Name+"OK");
                                }
                            }
                            if (FrameManager.FrameManager.ProductSetting.保存NG图片)
                            {
                                if (imageTask.bResultOK==false)
                                {
                                    SaveRunningImage(imageTask.currentImage, FrameManager.FrameManager.ProductSetting.保存图片路径, imageTask.Name + "NG");
                                }
                            }
                            break;
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                System.Threading.Thread.Sleep(10);
            }
        }
        public static DateTime DateTimeDelFileTime;
        public static bool SaveRunningImage(HalconDotNet.HImage hImage, string strImagePath, string strImageName, bool bJpeg = true)
        {
            string strPathTemp = strImagePath + @"\";
            if (!Directory.Exists(strPathTemp))
            {
                Directory.CreateDirectory(strPathTemp);

            }
            string strFileName = strPathTemp + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + strImageName;
            if (bJpeg)
            {
                hImage.WriteImage("jpeg 20", 0, strFileName);
            }
            else
            {
                hImage.WriteImage("bmp", 0, strFileName);
            }
            DeleteOldFiles(strImagePath, 3);

            return true;
        }
        static void DeleteOldFiles(string dir, int days)
        {
            if ((DateTime.Now - DateTimeDelFileTime).TotalDays < 1)
            {
                return;
            }
            try
            {
                if ((DateTime.Now - DateTimeDelFileTime).Days > 1)
                {
                    DateTimeDelFileTime = DateTime.Now;
                    if (!Directory.Exists(dir) || days < 1)
                        return;
                    if (Directory.GetFileSystemEntries(dir).Length < 1000)
                        return;
                    var now = DateTime.Now;
                    foreach (var f in Directory.GetFileSystemEntries(dir).Where(f => File.Exists(f)))
                    {
                        var t = File.GetCreationTime(f);

                        var elapsedTicks = now.Ticks - t.Ticks;
                        var elapsedSpan = new TimeSpan(elapsedTicks);

                        if (elapsedSpan.TotalDays > days) File.Delete(f);
                    }

                }

            }
            catch (Exception)
            {
                // ignored
            }
        }
        private static void autoGlueTimeFunction()
        {
            int iStep = 0;
            while (true)
            {
                if (GluePathActioner.GluePathAction.GlueStart)
                {
                    switch (iStep)
                    {
                        case 0:
                            if (IOModule.IOManager.Output("GlueVa").ON)
                            {
                                GluePathActioner.GluePathAction.glueTimer.Start();
                                iStep = 10;
                            }
                            break;
                        case 10:
                            if (IOModule.IOManager.Output("GlueVa").OFF)
                            {
                                GluePathActioner.GluePathAction.dTotalTimeGlue = GluePathActioner.GluePathAction.dTotalTimeGlue + GluePathActioner.GluePathAction.glueTimer.Duration;
                                
                                iStep = 0;
                            }
                            break;
                        default:
                            break;
                    }
                }
                System.Threading.Thread.Sleep(10);
            }
        }
        private static bool MachineStatus_setHomeStart()
        {
            if (FrameManager.MachineStatus.bHoming)
                return false;
            if (XtraMessageBox.Show("是否原点回复", "提示", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return false;
            if (FrameManager.MachineStatus.bStart)
            {
                XtraMessageBox.Show("设备正在自动中无法回复原点", "提示", MessageBoxButtons.OKCancel);
                return false;
            }
                
            Task.Factory.StartNew(() =>
            {
                HomeFunction();
            });
            return true;
        }
        public static void HomeFunction()
        {
            zMotionCard.ZeroPos(0);
            zMotionCard.ZeroPos(1);
            zMotionCard.ZeroPos(2);
            zMotionCard.ZeroPos(3);
            //zMotionCard.ZeroPos(4);
            zMotionCard.ZeroPos(6);
            zMotionCard.ZeroPos(7);
            FrameManager.MachineStatus.bHomeed = false;
            FrameManager.MachineStatus.bHoming = true;
            TableModule.TableManager.tableRunner.tableActioner.bHomeOk = false;
            TableModule.TableManager.tableRunner.SetSpd("HomeSpd");
            //TableModule.TableManager.tableRunner.tableActioner.bHomeOk = false;
            //TableModule.TableManager.tableRunner.Home(TableModule.AxisName.Z);
            //System.Threading.Thread.Sleep(400);
            //TableModule.TableManager.tableRunner.SetPosAxis(TableModule.AxisName.Z, 0);
            //TableModule.TableManager.tableRunner.WaitMoveDoneALL();
            //TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.Z,GeneralDefine.MotionDir.CW);
            if(FrameManager.MachineStatus.Is4Axis)
            {
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.Y, GeneralDefine.MotionDir.CW);
                System.Threading.Thread.Sleep(2000);
                TableModule.TableManager.tableRunner.StopALL();
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.U);
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.SetPosAxis(TableModule.AxisName.U, 0);
            }



            if (FrameManager.MachineStatus.Is4Axis)
            {
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.X,GeneralDefine.MotionDir.CCW,false);
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.Y, GeneralDefine.MotionDir.CCW, false);
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.Z, GeneralDefine.MotionDir.CCW, false);
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.WaitMoveDoneALL();
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.X, false);
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.Y, false);
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.Z, false);
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.WaitMoveDoneALL();
            }
            else
            {
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.Z, GeneralDefine.MotionDir.CCW, false);
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.WaitMoveDoneALL();
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.X, GeneralDefine.MotionDir.CCW, false);
                TableModule.TableManager.tableRunner.Jog(TableModule.AxisName.Y, GeneralDefine.MotionDir.CCW, false);
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.WaitMoveDoneALL();
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.Z);
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.X, false);
                TableModule.TableManager.tableRunner.Home(TableModule.AxisName.Y, false);
                System.Threading.Thread.Sleep(400);
                TableModule.TableManager.tableRunner.WaitMoveDoneALL();
            }
            System.Threading.Thread.Sleep(400);
            TableModule.TableManager.tableRunner.SetPosAxis(TableModule.AxisName.X, 0);
            TableModule.TableManager.tableRunner.SetPosAxis(TableModule.AxisName.Y, 0);
            TableModule.TableManager.tableRunner.SetPosAxis(TableModule.AxisName.Z, 0);

            System.Threading.Thread.Sleep(400);
            TableModule.TableManager.tableRunner.Go("SafePos");

            FrameManager.MachineStatus.bHoming = false;
            FrameManager.MachineStatus.bHomeed = true;
            TableModule.TableManager.tableRunner.tableActioner.bHomeOk = true;
            if(FrameManager.MachineStatus.Is4Axis)
                zMotionCard.ZeroPos(4);
            else
                zMotionCard.ZeroPos(3);

        }
        public static Form OpenFrameStart()
        {
            //if (FrameManager.FrameManager.FrmStart != null)
            //{
                
            //}
            //FormStart formStart = new FormStart();
            //FrameManager.FrameManager.FrmStart = formStart;
            return FrameManager.FrameManager.FrmStart;
        }
        public static Form OpenOperatorForm()
        {
            
            return new FormOperator();
        }
        public static Form OpenEnginerForm()
        {
            return new FormEngineer();
        }
        public static void MarkTableAngleToMin()
        {
            double dUCurrentU = TableManager.tableActioner.WhereTable().dUPos;
            dUCurrentU = ((int)(dUCurrentU * 1000) % 360000) / 1000.0;

            double dAngleTemp = dUCurrentU;
            int iAngleTemp = (int)(dAngleTemp * 1000.0);
            iAngleTemp = iAngleTemp % 360000;
            dAngleTemp = (double)iAngleTemp / 1000.0;
            if (dAngleTemp > 180)
            {
                dUCurrentU = dAngleTemp - 360.0;
            }
            else
            {
                if (dAngleTemp < -180)
                {
                    dUCurrentU = dAngleTemp + 360.0;
                }
                else
                {
                    dUCurrentU = dAngleTemp;
                }
            }
            TableManager.tableActioner.SetPosAxis(AxisName.U, dUCurrentU);
            System.Threading.Thread.Sleep(100);
        }

        public static ProductItem GetActionItem()
        {
            if (CustomManager.bRequestPositionMatch)
                return null;
            double dMinPos = TableManager.tableRunner.GetPosition("MinPos").dXPos;
            double dMaxPos = TableManager.tableRunner.GetPosition("MaxPos").dXPos;
            for (int i = ConveyerManager.productItems.Count - 1; i > -1; i--)
            {
                if (ConveyerManager.productItems[i].AreadyGlue == false)
                {
                    double dCurrentPos = (ConveyerManager.productItems[i].ImagePosX + ConveyerManager.CurrentPos - ConveyerManager.productItems[i].TrigerBeltPosX);
                    if (dCurrentPos > dMinPos && dCurrentPos < dMaxPos)
                    {
                        return ConveyerManager.productItems[i];
                    }
                }
                
            }
            return null;
        }
        public static bool HaveUnGlueItem()
        {
            double dMinPos = TableManager.tableRunner.GetPosition("MinPos").dXPos;
            for (int i = ConveyerManager.productItems.Count - 1; i > -1; i--)
            {
                double dCurrentPos = (ConveyerManager.productItems[i].ImagePosX + ConveyerManager.CurrentPos - ConveyerManager.productItems[i].TrigerBeltPosX);
                if (ConveyerManager.productItems[i].AreadyGlue==false)
                {
                    return true;
                }

            }
            return false;
        }
        public static void GlueWeightAction()
        {
            TableManager.tableRunner.SetSpd("HighSpd");
            TableManager.tableRunner.AbsGoXYU("称重位置");
            TableManager.tableRunner.SetSpd("LowSpd");
            TableManager.tableRunner.Go("称重位置");
            IOModule.IOManager.Output("GlueVa").ON = true;
            int iGlueTime = (int)ProjectModule.ProjectManager.ProjectSetting.SingleProductGlueTime * 1000;
            System.Threading.Thread.Sleep(iGlueTime);
            IOModule.IOManager.Output("GlueVa").OFF = true;

            System.Threading.Thread.Sleep(1000);
            TableManager.tableRunner.SetSpd("LowSpd");
            TableManager.tableRunner.AbsMoveSingle(AxisName.Z, "SafePos");
            TableManager.tableRunner.SetSpd("HighSpd");
            TableManager.tableRunner.Go("SafePos");

        }
    }
}
